package chapter20;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class TestPigComparator {
    public static void main(String[] args) {
        Pig pig1=new Pig("xiaomei","pink",152);
        Pig pig2=new Pig("xiaoli","white",110);
        Pig pig3=new Pig("lili","white",140);
        Pig pig4=new Pig("haila","black",220);
        List<Pig> pigsList = new ArrayList<Pig>();
        pigsList.add(pig1);
        pigsList.add(pig2);
        pigsList.add(pig3);
        pigsList.add(pig4);
        System.out.println("列表元素:");
        for (Pig person : pigsList){
            System.out.println(person.toString());
        }
        System.out.println("升序排序列表元素:");
        Collections.sort(pigsList,new PigObjectComparator());
        for (Pig person : pigsList){
            System.out.println(person.toString());
        }
        System.out.println("逆序排序列表元:");
        Collections.reverse(pigsList);
        for (Pig pig : pigsList){
            System.out.println(pig.toString());
        }
        System.out.println("随机序列表元素:");
        Collections.shuffle(pigsList);
        for (Pig pig : pigsList){
            System.out.println(pig.toString());
        }
        System.out.println("体重最大的对象:");
        System.out.println(Collections.max(pigsList,new PigObjectComparator()).toString());

    }
    public static Pig Max(Pig pig1, Pig pig2, Comparator<Pig> o){
        if (o.compare(pig1, pig2) > 0)
            return pig1;
        else
            return pig2;
    }
}
